package org.eclipse.football;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class OffPlayer {
	// Declare private member variables
	private String name;
	private String team;
	private int pass_yds;
	private int pass_tds;
	private int picks;
	private int rush_yds;
	private int rush_tds;
	private int rec_yds;
	private int rec_tds;
	private int fumbles;
	private int ret_tds;
	
	// Define constructor
	public OffPlayer(String name, String team){
		// Set name and team based off input
		this.name = name;
		this.team = team;
		
		// Initialize all other member variables to 0
		pass_yds = 0;
		pass_tds = 0;
		picks = 0;
		rush_yds = 0;
		rush_tds = 0;
		rec_yds = 0;
		rec_tds = 0;
		fumbles = 0;
		ret_tds = 0;
	}
	
	// Getters
	public String getName() {
		return name;
	}

	public String getTeam() {
		return team;
	}
	
	public int getPass_yds() {
		return pass_yds;
	}

	public int getPass_tds() {
		return pass_tds;
	}

	public int getPicks() {
		return picks;
	}

	public int getRush_yds() {
		return rush_yds;
	}

	public int getRush_tds() {
		return rush_tds;
	}

	public int getRec_yds() {
		return rec_yds;
	}

	public int getRec_tds() {
		return rec_tds;
	}
	
	public int getFumbles() {
		return fumbles;
	}
	
	public int getRet_tds() {
		return ret_tds;
	}

	// Setters
	public void setName(String name) {
		this.name = name;
	}
	
	public void setTeam(String team){
		this.team = team;
	}
	
	public void setPass_yds(int pass_yds) {
		this.pass_yds = pass_yds;
	}

	public void setPass_tds(int pass_tds) {
		this.pass_tds = pass_tds;
	}

	public void setPicks(int picks) {
		this.picks = picks;
	}

	public void setRush_yds(int rush_yds) {
		this.rush_yds = rush_yds;
	}

	public void setRush_tds(int rush_tds) {
		this.rush_tds = rush_tds;
	}

	public void setRec_yds(int rec_yds) {
		this.rec_yds = rec_yds;
	}

	public void setRec_tds(int rec_tds) {
		this.rec_tds = rec_tds;
	}

	public void setFumbles(int fumbles) {
		this.fumbles = fumbles;
	}
	
	public void setRet_tds(int ret_tds) {
		this.ret_tds = ret_tds;
	}
	
	// Define function to calculate points for this player
	public int calculatePoints(){
		// Initialize sum to 0
		int sum = 0;
		
		//TODO: JUST A MARKER FOR THIS MALARKY
		updatePerformanceFromFile(17);
		
		// Add points for passing stats
		sum += pass_yds/25;
		sum += 4*pass_tds;
		sum -= 2*picks;
		
		// Add points for rushing stats
		sum += rush_yds/10;
		sum += 6*rush_tds;
		
		// Add points for receiving stats
		sum += rec_yds/10;
		sum += 6*rec_tds;
		
		// Add points for miscellaneous stats
		sum += 6*ret_tds;
		sum -= 2*fumbles;
	
		// Return sum
		return sum;
	}
	
	// Define function to check the specified CSV for this player's
	// actual game performance
	public boolean updatePerformanceFromFile(Integer week){
		ArrayList<File> ourFiles = new ArrayList<File>();
		File rushing;
		File passing;
		File receiving;
		FileReader inputFileReader = null;
		for (int i=1;i<week;i++){
			rushing = new File("rushing" + i + "clean.txt");
			passing = new File("passing" + i + "clean.txt");
			receiving = new File("receiving" + i + "clean.txt");
			if ( rushing.exists()) {
				ourFiles.add(rushing);
			} else {
				System.err.println("rushing" + i + "clean.txt not found");
			}
			if ( passing.exists()) {
				ourFiles.add(passing);
			} else {
				System.err.println("passing" + i + "clean.txt not found");
			}
			if ( receiving.exists()) {
				ourFiles.add(receiving);
			} else {
				System.err.println("receiving" + i + "clean.txt not found");
			}
			//ourFiles.add(new File("kicking" + week + "clean.txt"));
		}
		pass_yds = 0;
		pass_tds = 0;
		picks    = 0;
		rush_yds = 0;
		rush_tds = 0;
		fumbles  = 0;
		rec_yds  = 0;
		rec_tds  = 0;
		fumbles  = 0;
		String theCurrentLine;
		ArrayList<String> theCurrentLineSplit;
		BufferedReader fr = null;
		Scanner sc = null;
		boolean found = false;
		String pos = null;
		for (File fileToCheck:ourFiles){
			try 
			{
				//Attach to file
				fr = new BufferedReader(new FileReader(fileToCheck));	
				//Read from file only if ready to be read
				while (fr.ready()){
					//Read line as String
					theCurrentLine= fr.readLine();
					//Split Line into parts to find the name at the front
					theCurrentLineSplit = new ArrayList<String>(Arrays.asList(theCurrentLine.split(",")));
					//Check the name at the start of the line to match this player
					if (theCurrentLineSplit.get(1).equalsIgnoreCase(name)){
						//Scanner == String Parser
						sc = new Scanner(theCurrentLine).useDelimiter(",");
						//EXAMPLE LINE:
						//1,Cam Newton, QB,CAR,28,46,432,1,3,3
						//Skip the rank at the front
						if (sc.hasNextInt()) { sc.nextInt(); }
						//Skip the name
						if (sc.hasNext())    { sc.next(); }
						//Skip the position
						if (sc.hasNext())    { pos = sc.next(); }
						//Skip the team
						if (sc.hasNext())    { sc.next(); }
						//Found him, set all member variables to the value in the file
						if (fileToCheck.toString().contains("passing")) {
							//Skip the attempts
							if (sc.hasNextInt()) { System.out.println(sc.nextInt()); }
							//Skip the completions
							if (sc.hasNextInt()) { System.out.println(sc.nextInt()); }
							if (sc.hasNextInt()) { pass_yds += sc.nextInt(); }
							if (sc.hasNextInt()) { pass_tds += sc.nextInt(); }
							if (sc.hasNextInt()) { picks    += sc.nextInt(); }
							//if (sc.hasNextInt()) { pass_2pt = sc.nextInt(); }
						}
						if (fileToCheck.toString().contains("rushing")) {
							//Skip the attempts
							if (sc.hasNextInt()) { sc.nextInt(); }
							if (sc.hasNextInt()) { rush_yds += sc.nextInt(); }
							if (sc.hasNextInt()) { rush_tds += sc.nextInt(); }
							if ( pos.contains("RB") || pos.contains("QB") ) { 
								if (sc.hasNextInt()) { fumbles  += sc.nextInt(); }
							}
							//if (sc.hasNextInt()) { rush_2pt = sc.nextInt(); }
						}
						if (fileToCheck.toString().contains("receiving")) {
							//Skip the receptions
							if (sc.hasNextInt()) { sc.nextInt(); }
							if (sc.hasNextInt()) { rec_yds  += sc.nextInt(); }
							if (sc.hasNextInt()) { rec_tds  += sc.nextInt(); }
							if ( pos.contains("WR") || pos.contains("TE")) { 
								if (sc.hasNextInt()) { fumbles  += sc.nextInt(); }
							}
							//if (sc.hasNextInt()) { rec_2pt  = sc.nextInt(); }
							//if (sc.hasNextInt()) { ret_tds  = sc.nextInt(); }
						}
						//if (fileToCheck.toString().contains("kicking")) {
						//}
						found = true;
						break;
					}
				}
			}
			catch (IOException ioe)
			{
				ioe.printStackTrace();
			}
			finally
			{
				if (fr != null && sc != null)
				{
					try
					{
						fr.close();
						sc.close();
					}
					catch (IOException ioe)
					{
						ioe.printStackTrace();
					}
				}
			}
		}
		if (!found){
			return false;
		}
		return true;
	}
}
